package com.gxa.meiye.admin.jms;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.meiye.admin.mapper.OrderMapper;
import com.gxa.meiye.admin.mapper.ShopOrderMapper;
import com.gxa.meiye.admin.service.OrderService;
import com.gxa.meiye.core.pojo.domain.Order;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zy
 * @CreateTime: 2022-07-23  10:39
 */
@Component
@Slf4j
public class OrderListen {
     @Autowired
    private OrderMapper orderMapper;
     @Autowired
     private ShopOrderMapper shopOrderMapper;
//    @JmsListener(destination = "orderCoupon",containerFactory = "factory")
//    public void orderCoupon(Message message)  {
//        try {
//            ActiveMQObjectMessage activeMQObjectMessage = (ActiveMQObjectMessage) message;
//            activeMQObjectMessage.setTrustAllPackages(true);
//            Serializable object = activeMQObjectMessage.getObject();
//
//            System.out.println(object.toString());
//            Order order = (Order) object;
//            order.setStatus(0);
//            //查询当前消费者的id
//            Integer select = orderMapper.select(order.getPhone());
//            if (select!=0){
//                //创建订单
//                Integer add = orderMapper.addCoupon(order);
//            }
//            message.acknowledge();
//
//        }catch (Exception e){
//            throw new RuntimeException(e);
//        }
////
//
//
//    }
    @JmsListener(destination = "orderCha",containerFactory = "factory")
    public void orderAdd(Message message)  {
        // 获取session的事务状态和ack的模式,测试完毕即可删除
        //System.out.println("事务状态:" + session.getTransacted());
        //System.out.println("ack模式:" + session.getAcknowledgeMode());
        try {


            ActiveMQObjectMessage activeMQObjectMessage = (ActiveMQObjectMessage) message;
            activeMQObjectMessage.setTrustAllPackages(true);
            Serializable object = activeMQObjectMessage.getObject();
            //System.out.println(s1);
            //String s = object.toString();


            System.out.println(object.toString());
            message.acknowledge();
            Order order = (Order) object;
            order.setStatus(1);
            //查询当前消费者的id
            Integer select = orderMapper.select(order.getPhone());
            if (select!=0){
                //创建订单
                if (order.getTherapist().length()==0){
                    order.setTherapist(null);
                }
                if (order.getTherapist()!=null){
                Integer selectTherapist = orderMapper.selectTherapist(order.getTherapist());

                    order.setTherapistId(selectTherapist.toString());
                }
                //门店
                if (order.getShopName().length()==0){
                    order.setShopName(null);
                }
                if (order.getShopName()!=null){
                Integer integer1 = shopOrderMapper.selectShop(order.getShopName());

                    order.setShopId(integer1.toString());
                }
                Integer integer = orderMapper.selectProject(order.getProject());

                order.setProjectId(integer);
                order.setUserId(select.toString());

                //创建时间
                order.setCreatedTime(LocalDateTime.now());
                Integer add = orderMapper.addOrder(order);
               // message.acknowledge();
            }




        }catch (Exception e){
            throw new RuntimeException(e);
        }
//


    }
}
